package net.zoneland.knowledge.controller.util;

import java.text.MessageFormat;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

import net.zoneland.knowledge.controller.bean.UomUserOrgQueryVo;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.PartyUser;
import net.zoneland.knowledge.service.PartyService;
import net.zoneland.knowledge.service.UserService;

/**
 * 数据查询工具类.
 * @author zmq
 * @date 2022/11/21 15:20
 */
public final class QaQueryUtils {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(QaQueryUtils.class);

    /**
     * 私有构造.
     */
    private QaQueryUtils() {

    }

    /**
     * 根据uid获取用户详细信息.
     * @param userId 用户uid
     * @param userService 用户接口
     * @param partyService 党员接口
     * @return 当前登录用户信息
     * @throws Exception
     */
    public static LoginUserPojo getLoginUserPojoByUserId(final String userId,
                                                         final UserService userService,
                                                         final PartyService partyService) throws Exception {
        Preconditions.checkArgument(StringUtils.isNotBlank(userId), "用户id不能为空！");
        final LoginUserPojo user = userService.getUserByUid(userId);
        Preconditions.checkNotNull(user, MessageFormat.format("用户id【{0}】找不到对应的用户", userId));

        //设置党员信息
        // 目前使用手机号比较合适，用员工编号查可能会有问题，1、员工编号格式有多种E开始的或者纯数字，2，党员信息中有没有员工编号的数据
        final UomUserOrgQueryVo queryVo = new UomUserOrgQueryVo();
        queryVo.setMobile(user.getMobilePhone());
        queryVo.setIgnoreHiddenInfo(true);
        final List<PartyUser> partyUserList = partyService.searchByCondition(queryVo);
        if (CollectionUtils.isNotEmpty(partyUserList)) {
            final PartyUser partyUser = partyUserList.get(0);
            user.setPartyUser(partyUser);
            user.setGanShi(partyUser.getGanShi());
            user.setPartyMember(true);
            user.setPartyOrgName(partyUser.getOrgName());
            user.setPartyOrgUuid(partyUser.getOrgUUID());
        } else {
            user.setGanShi(false);
            user.setPartyMember(false);
        }
        return user;
    }
}
